package com.mh.web.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.mh.domain.AppRole;
import com.mh.domain.AppUser;
import com.mh.domain.Invoice;
import com.mh.domain.Item;
import com.mh.domain.Product;
import com.mh.service.AppRoleService;
import com.mh.service.AppUserRoleService;
import com.mh.service.AppUserService;
import com.mh.web.utils.CustomGenericResponse;
import com.mh.web.utils.Page;


@Controller
@RequestMapping(value="/appuser" )
public class AppUserController {

	@Autowired
	private AppUserService appUserService;
	
	@Autowired
	private AppRoleService appRoleService;
	
	@Autowired
	private AppUserRoleService appUserRoleService;
	
	private Validator validator;
	
	@Autowired
	public AppUserController(Validator validator){
		this.validator = validator;		
	}
	
	
	@RequestMapping(method = RequestMethod.GET)
	public String getPage(){
		
		return "users";
	}
	
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public AppUser read(@PathVariable("id") Integer id){
		return appUserService.findAppUserByPrimaryKey(id);		
	}
		
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public @ResponseBody Page<AppUser> listUsers(AppUser appuser,
			@RequestParam(value = "page", required = false, defaultValue = "1") int page,
		      @RequestParam(value = "max", required = false, defaultValue = "20") int max){
		
		
		
		List<AppUser> lst = appUserService.findAppUser(appuser);
		Page<AppUser> pg = new Page<AppUser>();
		pg.setMax(max);
		pg.setPage(page);
		pg.setRows(lst);
		return pg;
	}
	
	
	
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.PUT)
	public @ResponseBody CustomGenericResponse delete(@PathVariable("id") int id){
		CustomGenericResponse resp = new CustomGenericResponse();
		try {
			AppUser appuser = appUserService.findAppUserByPrimaryKey(id);
			appUserService.deleteAppUser(appuser);
			resp.setSuccess(true);
			resp.setMessage("Success");
		} catch (Exception e) {
			resp.setSuccess(true);
			resp.setMessage("Error");
		}		
		return resp;
		
	}
	
	
	
	//
	@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
	public @ResponseBody CustomGenericResponse  update(
			@RequestBody AppUser appuser, @PathVariable("id") int id){
		CustomGenericResponse resp = new CustomGenericResponse();
		List<String> errors = validate(appuser);
		
		if(!errors.isEmpty()){
			resp.setSuccess(false);			
			resp.setMessages(errors);
			return resp;
		}		
		appUserService.saveAppUser(appuser);
		return resp;
	}
	
	@RequestMapping(value = "/save", method = RequestMethod.PUT)	
	public @ResponseBody CustomGenericResponse  save(
			@RequestBody AppUser appuser){
		CustomGenericResponse resp = new CustomGenericResponse();
		List<String> errors = validate(appuser);
		
		if(!errors.isEmpty()){
			resp.setSuccess(false);			
			resp.setMessages(errors);
			return resp;
		}		
		appUserService.saveAppUser(appuser);
		return resp;
	}
	
	/*detalle*/
	/*-- ----------------------------------------- --*/	
	
	
	/*-- ---------------------------------------- --*/
	
	
	private List<String> validate(AppUser appuser){
		List<String> errors = new ArrayList<String>();
		Set<ConstraintViolation<AppUser>> violations = validator.validate(appuser);
		for (ConstraintViolation<AppUser> violation : violations) 
			errors.add(violation.getMessage());			   
		return errors;		
	}
	
	private List<String> getMessages(BindingResult result){
		List<String> errors = new ArrayList<String>();
			for(ObjectError error: result.getAllErrors()){
				errors.add(error.getObjectName() + "-" + error.getCode() + " - "+  error.getDefaultMessage());
			}
		return errors;
	}
		

	public AppUserService getAppUserService() {
		return appUserService;
	}

	public void setAppUserService(AppUserService appUserService) {
		this.appUserService = appUserService;
	}
	

}//class
